package com.fay.designPattern.principles.dry;

import org.apache.commons.lang.StringUtils;

/**
 * @Title:
 * @Description:
 * @Author Fay Wang
 * @Date 2022/3/3 14:54
 * @Version 1.0
 */
public class IpChecker {
    public boolean isValidIp(String ipAddress) {
        if (StringUtils.isBlank(ipAddress)) {
            return false;
        }
        /*String regex = "^(1\\d{2}|2[0-4]\\d|25";
        return ipAddress.matches(regex);*/
        return false;
    }

    public boolean checkIfIpValid(String ipAddress) {
        if (StringUtils.isBlank(ipAddress)) {
            return false;
        }
        String[] ipUnits = StringUtils.split(ipAddress, ".");
        if (ipUnits.length != 4) {
            return false;
        }
        for (int i = 0; i < 4; ++i) {
            int ipUnitIntValue;
            try {
                ipUnitIntValue = Integer.parseInt(ipUnits[i]);
            } catch (NumberFormatException e) {
                return false;
            }
        }
        // test again
        return true;
    }
}
